const CONFIG = require('../../config.js');
const util = require('../../utils/util.js')
let currentDay = util.formatDate(new Date());

let TEXTData = {};

Page({
  data: {
    loginInfo: null,
    navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#488dea))',
    financingPreferenceList: [],
    workList: [ {name: '工薪族', id: '1'}, {name: '企业主', id: '2'}, {name: '自由职业', id: '3'}],
    hasList: [ {name: '有', id: '1'}, {name: '无', id: '0'} ],
    isList: [ {name: '是', id: '1'}, {name: '否', id: '0'} ],
    educationList: [ {name: '高中及以下', id: '1'},{name: '大专', id: '2'},{name: '本科', id: '3'},{name: '研究生', id: '4'},{name: '博士', id: '5'}],
	salaryList: [{name: '3000以下', id: '1'},{name: '3000-5000元', id: '2'},{name: '5001-8000元', id: '3'},{name: '8000元以上', id: '4'}],
	hourseList: [{name: '有红本无按揭与抵押', id: '1'},{name: '有红本按揭中', id: '2'},{name: '有红本抵押中', id: '3'},{name: '无红本', id: '4'}],
	houseStatusList: [{name: '持证满1年', id: '0'},{name: '持证满3个月', id: '1'},{name: '持证未满3个月', id: '2'},{name: '未拿到房产证', id: '3'}],
	houseTypeList: [{name: '商品房住宅', id: '1'},{name: '回迁房住宅', id: '2'},{name: '商铺', id: '3'},{name: '写字楼', id: '4'},{name: '公寓', id: '5'},{name: '其他', id: '6'}],
	carList: [{name: '无贷款', id: '1'},{name: '按揭中', id: '2'},{name: '抵押中', id: '3'}],
	creditList:[],
	creditCardList: [],
	maximumShowOverdueList: [{name: 'N', id: '0'},{name: '1', id: '1'},{name: '2', id: '2'},{name: '3', id: '3'},{name: '4', id: '4'},{name: '5', id: '5'},{name: '6', id: '6'},{name: '7', id: '7'}],
	overdueValueList: [{name: '500以下', id: '1'},{name: '500-1000', id: '2'},{name: '1000-1万', id: '3'},{name: '1万以上', id: '4'}],
	sixMonthSearchList: [{name: '没有查询', id: '1'},{name: '1-3次', id: '2'},{name: '4-6次', id: '3'},{name: '7-10次', id: '4'},{name: '11-15次', id: '5'},{name: '超过15次', id: '6'}],
	twoYearOverdueList: [{name: '没有查询', id: '1'},{name: '1-3次', id: '2'},{name: '4-6次', id: '3'},{name: '7-10次', id: '4'},{name: '11-15次', id: '5'},{name: '超过15次', id: '6'}],
	enterpriseTaxList: [{name: '1万以内', id: '1'},{name: '10001-3万', id: '2'},{name: '30001-5万', id: '3'},{name: '50001-10万', id: '4'},{name: '11-30万', id: '5'},{name: '30万以上', id: '6'}],
	enterTimeList: [{name: '0-3个月', id: '1'},{name: '4个月-6个月', id: '2'},{name: '7个月-12个月', id: '3'},{name: '13-24个月', id: '4'},{name: '24个月以上', id: '5'}],
	enterpriseList: [{name: '100万以内', id: '1'},{name: '100-199万', id: '2'},{name: '200-299万', id: '3'},{name: '300万以上', id: '4'}],
	insuranceTimeList: [{name: '1年1次', id: '1'},{name: '2年2次', id: '2'},{name: '2年3次', id: '3'},{name: '3年4次及以上', id: '4'}],
	enterpriseFlowList: [{name: '10万以内', id: '1'},{name: '11-50万', id: '2'},{name: '51-100万', id: '3'},{name: '101-300万', id: '4'},{name: '300万以上', id: '5'}],
    formData: {
		userName: '',
		userAge: '',
		phoneNo: '',
		applyCount: '',
		applyTime: currentDay,
		isMarry: '',
		education: '',
		useTerm: '',
		financingPreference: '',
		// 第二页
		occupation: '',
		isHouse: '',
		isCar: '',
		isInsurance: '',
		// 
		houseValue: '',
		houseStatus: '',
		carValue: '',
		carStatus: '',
		insuranceYearValue: '',
		salaryPayType: '',
		salaryPayValue: '',
		salarySocialSecurity: '',
		salarySocialSecurityValue: '',
		salaryHousingFund: '',
		salaryHousingFundValue: '',
		enterpriseTax: '',
		enterpriseTaxValue: '',
		enterpriseBilling: '',
		enterpriseBillingValue: '',
		enterpriseFlow: '',
		enterpriseFlowValue: '',
		isCredit: '',
		creditHaveArr: [],
		creditCardHaveArr: [],
		sixMonthSearch: '',
		twoYearOverdue: '',
		maximumShowOverdue: '',
		nowIsOverdue: '',
		overdueValue: '',
		houseCertificateStatus: '',
		houseType: '',
		enterpriseEstablishedTime: '',
		insuranceTime: '',
	},
	applyTime: currentDay,
	startDate: currentDay,
    isPage1: true,
    isPage2: false,
	isPage3: false,
	isPage4: false,
	isPage5: false,
	error: '',
	navImg: '',
	headTitle: '\u8d37\u6b3e',
	isTrueData: '1',
	TEXTDataList: [],
  },
  	// 单选
	radioChange: function (e) {
		const {field} = e.currentTarget.dataset;
		if(field === 'sixMonthSearch'){
			this.setData({
				[`formData.twoYearOverdue`]: '',
			})
		}
		this.setData({
			[`formData.${field}`]: e.currentTarget.dataset.value
		})
	},
	// 多选
	checkChange: function (e) {
		const {field} = e.currentTarget.dataset;
		let val = e.currentTarget.dataset.value;
		// 以上皆无 
		if((field === 'creditHaveArr' && val == '5') || (field === 'creditCardHaveArr' && val == '5')){
			this.setData({
				[`formData.${field}`]: [val]
			})
		}else{
			if(this.data.formData.creditHaveArr[0] === '5'){
				this.data.formData.creditHaveArr = [];
			}else if(this.data.formData.creditCardHaveArr[0] === '5'){
				this.data.formData.creditCardHaveArr = [];
			}

			let idx = this.data.formData[field].indexOf(val);
			if(idx > -1){
				this.data.formData[field].splice(idx, 1);
			}else{
				this.data.formData[field].push(val);
			}
			this.setData({
				[`formData.${field}`]: this.data.formData[field]
			})
		}
	},
	// 输入框
	formInputChange(e) {
		const {field} = e.currentTarget.dataset;
		let val = e.detail.value;
		if(field === 'useTerm' && val > 30){
			wx.showToast({ title: '用款期限不能超过30年', icon: 'none', duration: 2000 });
			val = '';
		}else if(field === 'phoneNo' && val.length > 11){
			wx.showToast({ title: '联系电话格式不对', icon: 'none', duration: 2000 });
			val = val.substring(0,11);
		}
		this.setData({
			[`formData.${field}`]: val
		})
	},
	// 时间选择
	bindDateChange: function (e) {
		this.setData({
			applyTime: e.detail.value,
			[`formData.applyTime`]: e.detail.value
		})
	},
	// 下一页
	nextForm: function (e) {
		let page = e.currentTarget.dataset.page;
		let datas = this.data.formData;
		let isBoolPage4 = datas.isHouse !== '1' && datas.isCar !== '1' && datas.isInsurance !== '1';
		if(page === "1"){
			let reg = /^1[0-9]{10}$/;
			let flag = reg.test(datas.phoneNo);
			if(!flag) {
				wx.showToast({ title: '联系电话格式不正确！', icon: 'none' })
				return;
			}
			if(datas.userName === '' || datas.userAge === '' || datas.phoneNo === '' || datas.applyCount === ''
			|| datas.applyTime === '' || datas.isMarry === '' || datas.education === '' || datas.useTerm === '' || datas.financingPreference === ''){
				wx.showToast({
					title: '请填写完整！',
					icon: 'none',
				})
				return;
			}
			this.setData({
				isPage1: false,
				isPage2: true,
				isPage3: false,
			})
		}else if(page === "2"){
			if(datas.occupation === '' || datas.isHouse === '' || datas.isCar === '' || datas.isInsurance === ''){
				wx.showToast({
					title: '请填写完整！',
					icon: 'none',
				})
				return;
			}
			if(datas.occupation === '3'){
				if(isBoolPage4){
					this.setData({
						isPage1: false,
						isPage2: false,
						isPage3: false,
						isPage4: false,
						isPage5: true,
					})
				}else{
					this.setData({
						isPage2: false,
						isPage3: false,
						isPage4: true,
						isPage5: false,
					})
				}
			}else{
				this.setData({
					isPage2: false,
					isPage3: true,
					isPage4: false,
				})
			}
		}else if(page === "3"){
			let isBool1 = (datas.occupation === '1') && (datas.salaryPayType === '' || datas.salarySocialSecurity === '' || datas.salaryHousingFund === '' ||
				(datas.salaryPayType === '1' && datas.salaryPayValue === '') ||
				(datas.salarySocialSecurity === '1' && datas.salarySocialSecurityValue === '') ||
				(datas.salaryHousingFund === '1' && datas.salaryHousingFundValue === ''));
			let isBool2 = (datas.occupation === '2') && (datas.enterpriseTax === '' || datas.enterpriseBilling === '' || datas.enterpriseFlow === '' ||
				(datas.enterpriseTax === '1' && datas.enterpriseTaxValue === '') ||
				(datas.enterpriseBilling === '1' && datas.enterpriseBillingValue === '') ||
				(datas.enterpriseFlow === '1' && datas.enterpriseFlowValue === ''));
			if(isBool1 || isBool2){
				wx.showToast({
					title: '请填写完整！',
					icon: 'none',
				})
				return;
			}
			if(isBoolPage4){
				this.setData({
					isPage1: false,
					isPage2: false,
					isPage3: false,
					isPage4: false,
					isPage5: true,
				})
			}else{
				this.setData({
					isPage1: false,
					isPage2: false,
					isPage3: false,
					isPage4: true,
					isPage5: false,
				})
			}
		}else if(page === "4"){
			let isBool = (datas.isHouse === '1' && (datas.houseValue === '' || datas.houseStatus === '')) ||
							(datas.isCar === '1' && (datas.carValue === '' || datas.carStatus === '')) ||
							(datas.isInsurance === '1' && datas.insuranceYearValue === '');
			if(isBool){
				wx.showToast({
					title: '请填写完整！',
					icon: 'none',
				})
				return;
			}
			this.setData({
				isPage1: false,
				isPage2: false,
				isPage3: false,
				isPage4: false,
				isPage5: true,
			})
		}else if(page === "5"){
			// 完成测算
            console.log(datas.creditHaveArr.indexOf('1'),1212);

			let isBool = (datas.isCredit === '') || 
						(datas.isCredit === '1' && (datas.creditHaveArr === '' || datas.creditCardHaveArr === '' || datas.sixMonthSearch === '' || datas.twoYearOverdue === '') ||
                            (datas.creditHaveArr.indexOf('1') != -1 && (datas.maximumShowOverdue === '' || datas.twoYearShowOverdue === '' || datas.nowIsOverdue === ''))
                            );
			if(isBool){
				wx.showToast({
					title: '请填写完整！',
					icon: 'none',
				})
				return;
			}
			this.submitFn()
		}
	},

	// 上一页
	lastForm: function (e){
		let page = e.currentTarget.dataset.page;
		if(page === "2"){
			this.setData({
				isPage1: true,
				isPage2: false,
				isPage3: false,
				isPage4: false,
			})
		}else if(page === "3"){
			this.setData({
				isPage1: false,
				isPage2: true,
				isPage3: false,
				isPage4: false,
				isPage5: false,
			})
		}else if(page === "4"){
			if(this.data.formData.occupation == '3'){
				this.setData({
					isPage2: true,
					isPage3: false,
					isPage4: false,
				})
			}else{
				this.setData({
					isPage2: false,
					isPage3: true,
					isPage4: false,
				})
			}
		}else if(page === "5"){
			this.setData({
				isPage1: false,
				isPage2: false,
				isPage3: false,
				isPage4: true,
				isPage5: false,
			})
			// 判断是否有房，车，保单
			if(this.data.formData.isHouse !== '1' && this.data.formData.isCar !== '1' && this.data.formData.isInsurance !== '1'){
				// 判断是否是自由职业
				if(this.data.formData.occupation == '3'){
					this.setData({
						isPage2: true,
						isPage3: false,
						isPage4: false,
						isPage5: false,
					})
				}else{
					this.setData({
						isPage3: true,
						isPage4: false,
						isPage5: false,
					})
				}
			}
		}
	},
	// 提交数据,保存快速申请单信息
	submitFn: function (){
		let datas = this.data.formData;
		datas.creditHave = datas.creditHaveArr.join(',');
		datas.creditHave = datas.creditHaveArr.length > 0 ? datas.creditHaveArr.join(',') : '';
		datas.creditCardHave = datas.creditCardHaveArr.length > 0 ? datas.creditCardHaveArr.join(',') : '';
		// let datas = {"userName":"zhangsan","userAge":"20","phoneNo":"18755556666","applyCount":"20","applyTime":"2021-01-23","isMarry":"1","education":"3","useTerm":"2","financingPreference":"2","occupation":"1","isHouse":"0","isCar":"0","isInsurance":"0","houseValue":"","houseStatus":"","carValue":"","carStatus":"","insuranceYearValue":"","salaryPayType":"0","salaryPayValue":"","salarySocialSecurity":"0","salarySocialSecurityValue":"","salaryHousingFund":"0","salaryHousingFundValue":"","enterpriseTax":"","enterpriseTaxValue":"","enterpriseBilling":"","enterpriseBillingValue":"","enterpriseFlow":"","enterpriseFlowValue":"","isCredit":"0","creditHaveArr":["3"],"creditCardHaveArr":["2"],"sixMonthSearch":"2","twoYearOverdue":"","creditHave":"3","creditCardHave":"2"};
		
		console.log(datas)
		wx.request({
			url: CONFIG.host + "/system/quickApplyInfoController/saveQuickApplyInfo",
			data: {
				productInfo : datas
			},
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
				})
				if(res.data.code === 'success'){
					wx.navigateTo({
						url: '../quickReport/quickReport?reportId=' + res.data.data
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	onLoad: function () {
		let showNum = wx.getStorageSync('showUser') === '0' ? '0' : '1';
		TEXTData = require('../../utils/text'+ showNum +'.js');
		TEXTData = TEXTData.textJson;

		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			let info = JSON.parse(loginInfo);
			this.setData({
				loginInfo : info,
				isTrueData: wx.getStorageSync('showUser'),
				financingPreferenceList : [ {name: TEXTData.dengebenjin, id: '1'}, {name: TEXTData.dengebenxi, id: '2'}, {name: TEXTData.xianxihouben, id: '3'}, {name: TEXTData.houxihouben, id: '4'}],
				creditList:[{name: TEXTData.yqL, id: '1'},{name: TEXTData.dzL, id: '2'},{name: '止付', id: '3'},{name: '关注', id: '4'},{name: '以上皆无', id: '5'}],
				creditCardList: [{name: TEXTData.yqL, id: '1'},{name: '止付', id: '2'},{name: TEXTData.djL, id: '3'},{name: TEXTData.dzL, id: '4'},{name: '以上皆无', id: '5'}],
				TEXTDataList: TEXTData
			})
			if(this.data.isTrueData == '0'){
				this.setData({
					headTitle : '\u8d37\u6b3e',
				})
			}
		}
	}
})
